//
//  NullView.m
//  CNstorm
//
//  Created by 陈家将 on 16/12/2.
//  Copyright © 2016年 chenjiajiang. All rights reserved.
//

#import "NullView.h"

@interface NullView ()

@property (weak, nonatomic) UIImageView *imgView;
@property (weak, nonatomic) UILabel *titleLab;
@property (weak, nonatomic) UILabel *detailLab;
@property (weak, nonatomic) UIButton *shoppingBtn;

@end

@implementation NullView

#pragma mark - **** instance ****

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectZero];
        imgView.image = [UIImage imageNamed:@"nullCart"];
        imgView.layer.cornerRadius = 50.0f;
        imgView.layer.masksToBounds = YES;
        [self addSubview:imgView];
        self.imgView = imgView;
        
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX);
            make.centerY.mas_equalTo(self.centerY).offset(-8*SPACE);
            make.width.mas_equalTo(100.0f);
            make.height.mas_equalTo(100.0f);
        }];
        
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLab.font = Font(15.0f);
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = COLOR_TEXT_GARY;
        [titleLab sizeToFit];
        [self addSubview:titleLab];
        self.titleLab = titleLab;
        
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(imgView.mas_bottom).offset(2 * SPACE);
            make.centerX.mas_equalTo(self.mas_centerX);
        }];
        
        UILabel *detailLab = [[UILabel alloc] initWithFrame:CGRectZero];
        detailLab.textColor = COLOR_TEXT_GARY;
        detailLab.textAlignment = NSTextAlignmentCenter;
        detailLab.font = Font(13.0f);

        [detailLab sizeToFit];
        [self addSubview:detailLab];
        self.detailLab = detailLab;
        
        [detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(titleLab.mas_bottom).offset(SPACE);
            make.centerX.mas_equalTo(self.mas_centerX);
        }];
        
        UIButton *shoppingBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        shoppingBtn.backgroundColor = COLOR_(@"ffad79");
        shoppingBtn.layer.cornerRadius = 3.0f;
        shoppingBtn.titleLabel.font = Font(15.0f);
        [shoppingBtn setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
        shoppingBtn.hidden = YES;
        [shoppingBtn addTarget:self action:@selector(gotoHomePage:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:shoppingBtn];
        self.shoppingBtn = shoppingBtn;
        
        [shoppingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(detailLab.mas_bottom).offset(2 * SPACE);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.width.mas_equalTo(120.0f);
            make.height.mas_equalTo(35.0f);
        }];
    }
    self.backgroundColor = COLOR_BG;
    return self;
}

#pragma mark - **** private method ****

- (void)gotoHomePage:(UIButton *)sender{
    if (_shoppingBtnBlock) {
        self.shoppingBtnBlock();
    }
}


#pragma mark - **** setter ****

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLab.text = title;
}

- (void)setDetial:(NSString *)detial {
    _detial = detial;
    self.detailLab.text = detial;
}

- (void)setBtnTitle:(NSString *)btnTitle {
    _btnTitle = btnTitle;
    self.shoppingBtn.hidden = !btnTitle;
    [self.shoppingBtn setTitle:btnTitle forState:UIControlStateNormal];
}

- (void)setIcon:(UIImage *)icon {
    _icon = icon;
    if (!icon) {
        self.imgView.image = icon;
    }else {
        self.imgView.image = [UIImage imageNamed:@"nullCart"];
    }
}

@end
